package graphics;


import processing.core.PApplet;
import processing.core.PFont;
import util.Movement;

/**
 *
 * @author Emanuel Quintero
 */
public class Tile {

    private PApplet parent;
    private int number;
    private int size;
    private double xpos;
    private double ypos;

    public Tile(PApplet parent, int number, int xpos, int ypos, int size) {
        this.parent = parent;
        this.number = number;
        this.size = size;
        this.xpos = xpos;
        this.ypos = ypos;
    }

    public void display() {
        parent.stroke(0, 19, 105);
        parent.strokeWeight((int) (0.06 * size));
        parent.smooth();
        parent.strokeJoin(PApplet.ROUND);
        parent.fill(15, 151, 242);
        parent.rect((int) xpos, (int) ypos, size, size);
        PFont f = parent.createFont("Arial Bold", 2 * size / 3, true);
        parent.textFont(f);
        parent.fill(255);
        parent.textAlign(PApplet.CENTER);
        parent.text("" + number, (int) xpos + size / 2, (int) ypos + 3 * size / 4);
    }

    public double getXPos() {
        return xpos;
    }

    public double getYPos() {
        return ypos;
    }

    public void setPos(double xpos, double ypos) {
        this.xpos = xpos;
        this.ypos = ypos;
    }

    public boolean movement(Movement mov, double speed, double goalState) {
        //Movimiento derecha/izquierda (en eje x)
        double remainingDist;
        if (mov==Movement.LEFT || mov==Movement.RIGHT) {
            remainingDist = Math.abs(goalState - xpos);
            speed = (remainingDist < speed) ? remainingDist : speed;
            speed = (mov==Movement.LEFT) ? speed : (-speed);
            xpos += speed;
            return xpos == goalState;
        } //Movimiento arriba/abajo (en eje y)
        else {
            remainingDist = Math.abs(goalState - ypos);
            speed = (remainingDist < speed) ? remainingDist : speed;
            speed = (mov == Movement.UP) ? speed : (-speed);
            ypos += speed;
            return ypos == goalState;
        }
    }
}
